package com.zhipu.balance.ui.vm.home.calculate.rice

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 整精米
 */
class RiceFinishingViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 稻谷实体
     */
    var mRecordRice : RecordRice? = null

    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null

    /**
     * 整精米率-试样质量
     */
    var qualityOfMilledRiceSample = ObservableField("")
    /**
     * 整精米质量
     */
    var headRiceQuality = ObservableField("")
    /**
     * 整精米率-结果
     */
    var headRiceResult = ObservableField("")



    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRice?.headRiceRecorder)){
            //整精米率-试样质量
            qualityOfMilledRiceSample.set(mRecordRice?.qualityOfMilledRiceSample.toString())
            //整精米质量
            headRiceQuality.set(mRecordRice?.headRiceQuality.toString())
            //整精米率-结果
            headRiceResult.set(mRecordRice?.headRiceResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //整精米率-试样质量
        qualityOfMilledRiceSample.set("")
        //整精米质量
        headRiceQuality.set("")
        //整精米率-结果
        headRiceResult.set("")
    }



    /**
     * 刷新数据
     */
    fun refreshData(){
        qualityOfMilledRiceSample.set(mRecordRice?.roughSampleQuality.toString())
        mRecordRice?.qualityOfMilledRiceSample = qualityOfMilledRiceSample.get()!!.toDouble()
    }


    /**
     * 试样质量
     */
    fun BulkQuality(){
        qualityOfMilledRiceSample.set(weighing)
        mRecordRice?.qualityOfMilledRiceSample = qualityOfMilledRiceSample.get()?.toDouble()
    }

    /**
     * 整精米质量
     */
    fun QualityMilledRice(){
        headRiceQuality.set(weighing)
        mRecordRice?.headRiceQuality = headRiceQuality.get()?.toDouble()
    }





    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算整精米率
         */
        var mMilledRiceRate = CommonUtils.formatDouble(mRecordRice!!.headRiceQuality / mRecordRice!!.qualityOfMilledRiceSample * 100)
        mRecordRice?.headRiceResult = mMilledRiceRate
        headRiceResult.set(mMilledRiceRate.toString())

        /**
         * 整精米率记录员
         */
        mRecordRice!!.headRiceRecorder = mPersonnel!!.name


        if(mRecordRice?.qualityOfMilledRiceSample == 0.00){
            qualityOfMilledRiceSample.set("0.00")
        }
        if(mRecordRice?.headRiceQuality == 0.00){
            headRiceQuality.set("0.00")
        }
    }
}